package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ITourismAccommodationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismAccommodation;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 民宿信息Controller
 *
 * @author gqd
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/system/accommodation")
public class TourismAccommodationController extends BaseController
{
    @Resource
    private ITourismAccommodationService tourismAccommodationService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询民宿信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:accommodation:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismAccommodation tourismAccommodation)
    {
        startPage();
        List<TourismAccommodation> list = tourismAccommodationService.selectTourismAccommodationList(tourismAccommodation);
        return getDataTable(list);
    }

    /**
     * 导出民宿信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:accommodation:export')")
    @Log(title = "民宿信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismAccommodation tourismAccommodation)
    {
        List<TourismAccommodation> list = tourismAccommodationService.selectTourismAccommodationList(tourismAccommodation);
        ExcelUtil<TourismAccommodation> util = new ExcelUtil<TourismAccommodation>(TourismAccommodation.class);
        return util.exportExcel(list, "accommodation");
    }

    /**
     * 获取民宿信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:accommodation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tourismAccommodationService.selectTourismAccommodationById(id));
    }

    /**
     * 新增民宿信息
     */
    @PreAuthorize("@ss.hasPermi('system:accommodation:add')")
    @Log(title = "民宿信息", businessType = BusinessType.INSERT)
    @PostMapping //TourismAccommodation tourismAccommodation
    public AjaxResult add(@RequestBody TourismAccommodation tourismAccommodation )
    {
        return toAjax(tourismAccommodationService.insertTourismAccommodation(tourismAccommodation));
    }

    /**
     * 修改民宿信息
     */
    @PreAuthorize("@ss.hasPermi('system:accommodation:edit')")
    @Log(title = "民宿信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismAccommodation tourismAccommodation)
    {
        return toAjax(tourismAccommodationService.updateTourismAccommodation(tourismAccommodation));
    }

    /**
     * 删除民宿信息
     */
    @PreAuthorize("@ss.hasPermi('system:accommodation:remove')")
    @Log(title = "民宿信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tourismAccommodationService.deleteTourismAccommodationByIds(ids));
    }

    /**
     * 查询所有民宿
     * @param map
     * @return
     */
    @PostMapping("/selectAll")
    public AjaxResult selectAll(@RequestBody Map map){
        String hotelName = (String) map.get("hotelName");
        return tourismAccommodationService.selectAll(hotelName);
    }


    /**
     * 根据民宿id查询民宿的详细信息
     * @param map
     * @return
     */
    @PostMapping("/selectByHotelId")
    public AjaxResult selectByHotelId(@RequestBody Map map, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser != null){
            Integer userId = loginUser.getUserId();
            Integer hotelId = (Integer) map.get("hotelId");
            return tourismAccommodationService.selectByHotelId(hotelId,userId);
        }else {
            Integer userId = null;
            Integer hotelId = (Integer) map.get("hotelId");
            return tourismAccommodationService.selectByHotelId(hotelId,userId);
        }
    }

    /**
     * 精选民宿的查询
     * @param map
     * @return
     */
    @PostMapping("/selectStatus")
    public AjaxResult selectStatus(@RequestBody Map map){
        String hotelName = (String) map.get("hotelName");
        return tourismAccommodationService.selectStatus(hotelName);
    }
}
